/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.ce.commands.user;

import com.bionic.ce.beans.User;
import com.bionic.ce.businesslogic.UserService;
import com.bionic.ce.commands.ICommand;
import com.bionic.ce.manager.Config;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author MAXIM
 */
public class CommandUpdateCurrentUserInfo implements ICommand {

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String page = null;
        Integer userId=Integer.parseInt(request.getParameter("userId"));
        String name = request.getParameter("Name");
        String surname = request.getParameter("Surname");
        String email = request.getParameter("email");
        String password = request.getParameter("Password");
        String adress = request.getParameter("Adress");
        String telephone = request.getParameter("Telephone");
        try {
            UserService.updateCurrentUser(userId, name, surname, email, password, adress, telephone);
            User user = UserService.returnCurrentUserInfo(userId);
            request.getSession().setAttribute("user", user);
            page = Config.getInstance().getProperty(Config.MAIN);
        } catch (IllegalStateException ex) {
            ex.printStackTrace();
            request.setAttribute("userupdateerror", "Updates was not effected because not all required fields were filled");
            page = Config.getInstance().getProperty(Config.GOTOUPDATE);
            request.setAttribute("Name", name);
            request.setAttribute("Surname", surname);
            request.setAttribute("email", email);
            request.setAttribute("Password", password);
            request.setAttribute("Adress", adress);
            request.setAttribute("Telephone", telephone);
        }
        return page;
    }
}